package cn.iocoder.yudao.module.shop.category.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.util.TreeUtils;
import cn.iocoder.yudao.module.shop.category.pojo.bo.ShopCategoryCodeBO;
import cn.iocoder.yudao.module.shop.category.pojo.po.ShopCategory;
import cn.iocoder.yudao.module.shop.category.pojo.vo.AppShopCategoryRespVO;
import cn.iocoder.yudao.module.shop.category.pojo.vo.TreeShopCategoryRespVO;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 行业类目")
@RestController
@RequestMapping("/shop/category")
@Validated
public class AppShopCategoryController {

    @Resource
    private ShopCategoryService shopCategoryService;

    @GetMapping("/all-codes")
    @Operation(summary = "已定义的所有行业类目标识")
    @PermitAll
    public CommonResult<List<ShopCategoryCodeBO>> getAllCode() {
        List<ShopCategoryCodeBO> allIdentify = shopCategoryService.getAllCode();
        return success(allIdentify);
    }

    @GetMapping("/list")
    @Operation(summary = "获得行业类目列表", description = "只包含直接的下级类目，parentId=0表示主营类目。")
    @Parameter(name = "parentId", description = "父类目编号", example = "0")
    @Parameter(name = "parentCode", description = "父类目编码", example = "mall")
    @PermitAll
    public CommonResult<List<AppShopCategoryRespVO>> getShopCategoryList(@RequestParam(value = "parentId", required = false) Long parentId, @RequestParam(value = "parentCode", required = false) String parentCode) {
        List<ShopCategory> list;
        if (parentId != null && parentId > 0) {
            list = shopCategoryService.getEnableCategoryList(parentId);
        } else if (StrUtil.isNotBlank(parentCode)) {
            list = shopCategoryService.getEnableCategoryList(parentCode);
        } else {
            list = shopCategoryService.getEnableRootCategoryList();
        }
        return success(BeanUtils.toBean(list, AppShopCategoryRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得行业类目列表（树形）", description = "包含不限层级的所有下级类目")
    @Parameter(name = "parentCode", description = "父类目编码", required = true, example = "mall")
    @PermitAll
    public CommonResult<List<TreeShopCategoryRespVO>> getShopCategoryTree(@RequestParam(value = "parentCode", defaultValue = "mall") String parentCode, @RequestParam(value = "includeParent", defaultValue = "false", required = false) Boolean includeParent) {
        if (includeParent == null) {
            includeParent = false;
        }
        List<ShopCategory> list = shopCategoryService.getEnableDescendentCategoryList(parentCode, includeParent);
        List<TreeShopCategoryRespVO> list2 = BeanUtils.toBean(list, TreeShopCategoryRespVO.class);
        return success(TreeUtils.buildTreeList(list2));
    }

}
